/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.data.controller.DistributionLnCtlr;
import org.gov.vis.data.controller.DistributionLnCtlrImpl;
import org.gov.vis.data.dto.DistributionLnDto;
import org.gov.vis.data.gui.EditDistributionLNDialog;
import org.gov.vis.data.gui.ViewDistributionLNDialog;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ViewDistributionLNListener implements ActionListener,BaseListener {
	


	
	private DistributionLnCtlr distributionLNCtlr = new DistributionLnCtlrImpl();
	private ViewDistributionLNDialog viewDistributionLNDialog;
	private DistributionLnDto distributionLNDto;
	final private ResourceBundle rb = Util.getResources();
	private EditDistributionLNDialog editDistributionLNDialog;

	public ViewDistributionLNListener(ViewDistributionLNDialog viewDistributionLNDialog) {
		// TODO Auto-generated constructor stub
		this.viewDistributionLNDialog = viewDistributionLNDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewDistributionLNDialog _viewDistributionLNDialog =(ViewDistributionLNDialog)_guiObject;
		distributionLNDto = new DistributionLnDto();
		
		String code = _viewDistributionLNDialog.getCodeTxtBx().getText();
		if(code!=null){
		 distributionLNDto.setCode(code);
		}
		/*otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());*/
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		DistributionLnDto _distributionLnDto = (DistributionLnDto)_dtoObject;
		if(editDistributionLNDialog != null){
			editDistributionLNDialog.getTxtFldCode().setText(_distributionLnDto.getCode());
			editDistributionLNDialog.getTxtFldDLNConvertedLtrs().setText(_distributionLnDto.getCnvtdLtrs());
			editDistributionLNDialog.getDtChoDateOfDistribution().setDate(_distributionLnDto.getDoDstrbut());
			editDistributionLNDialog.getTxtFldDLNEvoporation().setText(_distributionLnDto.getEvopLoss());
			editDistributionLNDialog.getTxtFldDLNFromStation().setText(_distributionLnDto.getFrmStation());
			editDistributionLNDialog.getTxtFldDLNKgs().setText(_distributionLnDto.getKgs());
			editDistributionLNDialog.getTxtFldDLNVehicalNo().setText(_distributionLnDto.getVehicleNo());
			editDistributionLNDialog.setHidnObject(_distributionLnDto.getDisLnId());
		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		
		
		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewDistributionLNDialog);
			try {
				
			List<DistributionLnDto> distributionLNLst = distributionLNCtlr.searchDisLNs(distributionLNDto);
			
			List<String> colLst = new ArrayList<String>();
			colLst.add("Code");
			colLst.add("Date of Distribution");
			//colLst.add("Kgs");
			colLst.add("From Station");
			colLst.add("Vehicle No");
			
			ArrayList<Object> dataList = new ArrayList<Object>(1);
			if(distributionLNLst != null && !distributionLNLst.isEmpty()){
				for(DistributionLnDto dto:distributionLNLst){
					ArrayList<Object> lst = new ArrayList<Object>(1);
					//setting each property of the current Accessory object (row) to the list
					lst.add(dto);
					lst.add(dto.getCode());
					lst.add(dto.getDoDstrbut());
					//lst.add(dto.getKgs());
					lst.add(dto.getFrmStation());
					lst.add(dto.getVehicleNo());
					dataList.add(lst);
				}//end for 
			}//end if
			VISDataGrid.createDataPanel(dataList, colLst, viewDistributionLNDialog.getPnlDistributionLNResults(),this);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			DistributionLnDto distributionLNDto = (DistributionLnDto)visHyperLink.getObject();
			//System.out.println("action from the object----"+distributionLNDto.getCode());
			editDistributionLNDialog = new EditDistributionLNDialog(Main.getMainFrame(),
                    rb.getString("dialog_edit_dist_ln_title"),
                    false);
			setDtoDetailsToGui(distributionLNDto);
			viewDistributionLNDialog.dispose();

		}
		
	}





}
